package com.example.shushiyunsst.fragment.sigin

import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.viewpager.widget.ViewPager
import com.example.library_common.base.BaseFragmentEx
import com.example.shushiyunsst.R
import com.google.android.material.tabs.TabLayout

/**
 * 登录与注册
 * */
class SignPageFragment : BaseFragmentEx(), SignPageListener {

    companion object {
        fun newInstance(): SignPageFragment {
            val args = Bundle()
            val fragment = SignPageFragment()
            fragment.arguments = args
            return fragment
        }
    }

    private lateinit var mViewPager: ViewPager

    private val pageLs = listOf<BaseFragmentEx>(
        SignInFragment.newInstance().setSignPageListener(this),
        SignUpFragment.newInstance().setSignPageListener(this),
    )

    override fun getLayoutId(): Any {
        return R.layout.sign_page_fragment
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mViewPager = view.findViewById(R.id.mViewPager)
        val mTabLayout: TabLayout = view.findViewById(R.id.mTabLayout)

        mViewPager.adapter = object :
            FragmentStatePagerAdapter(childFragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            override fun getCount(): Int {
                return pageLs.size
            }

            override fun getItem(position: Int): Fragment {
                return pageLs[position]
            }

            override fun getPageTitle(position: Int): CharSequence? {
                val lab = getItem(position) as LabInfo
                return lab.labTitle()
            }

        }

        mTabLayout.setupWithViewPager(mViewPager)
    }

    override fun intoSignIn() {
        mViewPager.currentItem = 0
    }

    override fun intoSignUp() {
        mViewPager.currentItem = 1
    }

    override fun onBackPressedSupport(): Boolean {
        _mActivity.finish()
        return true
    }
}